﻿using System;

namespace zijian666;

/// <inheritdoc/>
internal class MappingRule<TSource, TTarget> : IMappingRule
{
    private Action<TSource, TTarget> _map;

    public MappingRule(Action<TSource, TTarget> map)
    {
        _map = map ?? throw new ArgumentNullException(nameof(map));
    }

    /// <summary>
    /// 添加映射规则
    /// </summary>
    public void AddRule(Action<TSource, TTarget> map)
    {
        _map += map ?? throw new ArgumentNullException(nameof(map));
    }

    /// <inheritdoc/>
    public void Execute(object source, object target)
    {
        if (source is TSource s && target is TTarget t)
        {
            _map(s, t);
        }
    }
}
